<template>
    <div class="RoleSelector">
        <el-select :value="valueUse" @input="change" placeholder="请选择角色">
            <el-option label="所有角色" :value="0" v-if="isFilter"></el-option>
            <el-option v-for="item in roles"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
            ></el-option>
        </el-select>
    </div>
</template>

<script lang="ts">
import { Component, Prop, Watch, Vue } from 'vue-property-decorator'
import Api from "@/assets/api/Api"

@Component({
  components: {}
})
export default class RoleSelector extends Vue {
  @Prop({
    default: 0
  })
  readonly value!: any
  @Prop({
    default: false
  })
  readonly isFilter!: any
  roles: Array<any> = []

  mounted() {
    this.reload()
  }

  reload() {
    this.refreshList()
    this.change(this.isFilter ? 0 : null)
  }

  change(v) {
    this.$emit('input', v)
  }

  refreshList() {
    this.$ajax.request(Api.role_auth.roleAll).then(resp => {
      this.roles = resp
    })
  }

  get valueUse() {
    if (this.value) {
      return this.value
    }

    return this.isFilter ? 0 : null
  }

}
</script>

<style lang="less" scoped>
    @import (reference) "~style/all.less";

    .RoleSelector {
    }
</style>